package cmdb

import (
	"errors"
	"k8s-platform/dao"
	"k8s-platform/model"
)

var Credential credential

type credential struct{}

// 列表
func (*credential) List() ([]*model.Credential, error) {
	return dao.Credential.List()
}

// 查询单个
func (*credential) Has(credential string) (*model.Credential, bool, error) {
	return dao.Credential.Has(credential)
}

// 查询单个
func (*credential) Get(id int64) (*model.Credential, bool, error) {
	return dao.Credential.Get(id)
}

// 新增
func (*credential) Add(credential *model.Credential) error {
	_, has, err := dao.Credential.Has(credential.Name)
	if err != nil {
		return err
	}
	if has {
		return errors.New("该数据已存在，请重新添加")
	}

	if err := dao.Credential.Add(credential); err != nil {
		return err
	}
	return nil
}

// 更新
func (*credential) Update(credential *model.Credential) error {
	return dao.Credential.Update(credential)
}

// 删除
func (*credential) Delete(credential *model.Credential, id int64) error {
	return dao.Credential.Delete(credential, id)
}
